<?php
namespace Admin;
use Zend\ModuleManager\Feature;
use Zend\Loader;
use Zend\EventManager\EventInterface;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;

class Module implements Feature\AutoloaderProviderInterface,
    Feature\ConfigProviderInterface,
    Feature\ServiceProviderInterface,
    Feature\BootstrapListenerInterface,
    Feature\FormElementProviderInterface
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    /**
     * @{inheritdoc}
     */
    public function getServiceConfig()
    {
        return array(
            'invokables' => array(
                'admin_user_service' => 'Admin\Service\AdminUser',
                'Admin\Authentication\Adapter\Db' => 'Admin\Authentication\Adapter\Db',
                'Admin\Authentication\Storage\Db' => 'Admin\Authentication\Storage\Db',

                /*----------------------------------------Service-Config-Category---------------------------------------*/
                'admin_category_service'     => 'Admin\Service\AdminCaterory',
                /*----------------------------------------Service-Config-Category---------------------------------------*/

            ),
            'factories' => array(
                'admin_module_options'      => 'Admin\Factory\ModuleOptionsFactory',
                'admin_navigation'          => 'Admin\Navigation\Service\AdminNavigationFactory',
                //'user_mapper'           => 'Admin\Factory\UserMapperFactory',
                'admin_create_form'         => 'Admin\Factory\Form\CreateUserFormFactory',
                'admin_user_mapper'         => 'Admin\Factory\AdminUserMapperFactory',
                'admin_role_mapper'         => 'Admin\Factory\AdminRoleMapperFactory',
                'user_role_hydrator'        => 'Admin\Factory\Mapper\UserRoleHydratorFactory',
                'admin_update_form'         => 'Admin\Factory\Form\UpdateUserFormFactory',
                'admin_auth_service'                          => 'Admin\Factory\AuthenticationServiceFactory',
                'Admin\Authentication\Adapter\AdapterChain'   => 'Admin\Authentication\Adapter\AdapterChainServiceFactory',

                /*----------------------------------------Service-Config-Category---------------------------------------*/
                'admin_category_create_form'        => 'Admin\Factory\Form\Category\CreateCategoryFormFactory',
                'admin_category_mapper'             => 'Admin\Factory\Category\AdminCategoryMapperFactory',
                'category_hydrator'                 => 'Admin\Factory\Mapper\Category\CategoryHydratorFactory',
                /*----------------------------------------Service-Config-Category---------------------------------------*/
            ),
        );
    }

    public function getFormElementConfig()
    {
        return array(
            'factories' => array(
                /*'CreateCategoryFieldset' => function($sm){
                    $serviceLocator = $sm->getServiceLocator();
                    return new \Admin\Form\Category\CreateCategoryFieldset($serviceLocator);
                },*/
                'CreateCategoryFieldset' => 'Admin\Form\Category\CreateCategoryFieldset',
                'create_category_fieldset'          => function($sm){
                    $serviceLocator = $sm->getServiceLocator();
                    $createCategoryFieldset = new \Admin\Form\Category\CreateCategoryFieldset($serviceLocator);
                    return $createCategoryFieldset;
                },
            ),
        );
    }

    public function getControllerPluginConfig()
    {
        return array(
            'factories' => array(
                'AdminAuthentication' => 'Admin\Factory\Controller\Plugin\AdminAuthenticationFactory',
            ),
        );
    }
    
    public function getViewHelperConfig()
    {
        return array(
            'invokables' => array(
                //'form_button' => 'Admin\Form\View\Helper\FormButton',
            )
        );
    } 

    /**
     * @{inheritdoc}
     */
    public function onBootstrap(EventInterface $e)
    {
        /*
        $app = $e->getParam('application');
        $em  = $app->getEventManager();

        $em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'selectLayoutBasedOnRoute'));


        $em->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatchError'), -100);
        $em->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -100);
        $em->attach(MvcEvent::EVENT_RENDER_ERROR, array(
            $this,
            'onDispatchError'
        ), - 100);*/
    }

    /**
     * Select the admin layout based on route name
     *
     * @param  MvcEvent $e
     * @return void
     */
    public function selectLayoutBasedOnRoute(MvcEvent $e)
    {
        /*
        $app    = $e->getParam('application');
        $sm     = $app->getServiceManager();
        $config = $sm->get('config');

        if (false === $config['admin']['use_admin_layout']) {
            return;
        }

        $match      = $e->getRouteMatch();
        $controller = $e->getTarget();
        if (!$match instanceof RouteMatch
            || 0 !== strpos($match->getMatchedRouteName(), 'admin')
            || $controller->getEvent()->getResult()->terminate()
        ) {
            return;
        }


        $layout     = $config['admin']['admin_layout_template'];
        
        //$controller->layout($layout);*/
    }


    public function onDispatchError(MvcEvent $event){

        $response = $event->getResponse();
        if ($response->getStatusCode() == 404) {            
            //$serviceManager = $event->getApplication()->getServiceManager();
            //$layout = $serviceManager->get( 'viewManager' )->getViewModel();
            //$layout ->setTemplate('backend/layout');
        } elseif($response->getStatusCode() == 500){
            //DO SOMETHING else?
        }
    }
}
